var conditionSettingNightSet = {
	init : function() {
		$('.tab-alarm').height(nav.whiceH[1]);
		this.queryTabInit = this.quaryTab();
		this.validata=true;
		this.msg="";
		this.choseCar();
		this.annal();
		this.delId="";
		this.delAnalyseGroupsId="";
		this.allVehicleId = "";
		this.allVehicleHtml = "";// 用来储存用户拥有的车辆
		this.initEvent();
		this.alarmId = '';
		this.loadUserVehicle();
	},loadUserVehicle :function(keyWord){
		/**
		 * 加载未设置报警条件的车辆
		 */
		$.ajax({
			type : 'post',
			async : false,
			url : appCtx + "reportset/nightSet/loadUserVehicle.html",
			cache : false,
			data:{"analyseID":"7","keyWord":keyWord},
			dataType : "JSON",
			success : function(data) {
				if(data.vehicleList &&  data.vehicleList.length>0){
			
				
				var arr = data.vehicleList;
				var size = arr.length;
				var html = "";
				conditionSettingNightSet.allVehicleId = "";
				for (var i = 0; i < size; i++) {
					if (i == 0) {
						conditionSettingNightSet.allVehicleId += arr[i].vehicleId
								+ ","
						html += '<li class="'
								+ 'list active"><a class=' + '"db'
								+ '" href=' + '"javascript:;' + '">'
								+ arr[i].registrationNO
								+ '</a> <div style="'
								+ 'display: none"' + '>'
								+ arr[i].vehicleId + ',</div></li>'
					} else {
						conditionSettingNightSet.allVehicleId += arr[i].vehicleId
								+ ","
						html += '<li class="' + 'list "><a class='
								+ '"db' + '" href=' + '"javascript:;'
								+ '">' + arr[i].registrationNO
								+ '</a> <div style="'
								+ 'display: none"' + '>'
								+ arr[i].vehicleId + ',</div></li>'
					}
				}
				conditionSettingNightSet.allVehicleHtml = html;
				$(".queryCarCont").html(html);
			}
			}
		});
	},
	initEvent : function() {

		$("#allVehicle").on("click", function() {
			var obj = document.getElementById("vehicleTab");
			obj.style.display = "none";
		})
		$("#chooseVehicle").on("click", function() {
			var obj = document.getElementById("vehicleTab");
			obj.style.display = "block";
		})

		$("#vehicleSpeed").on("click", function() {
			$.ajax({
				type : 'post',
				async : false,
				url : appCtx + "reportset/vehicleSpeed/vehicleSpeedIndex.html",
				cache : false,
				dataType : "JSON",
				success : function(data) {

				}
			});
		})
		$("#nightSet").on("click", function() {
			$.ajax({
				type : 'post',
				async : false,
				url : appCtx + "reportset/nightSet/nightsetIndex.html",
				cache : false,
				dataType : "JSON",
				success : function(data) {

				}
			});
		})

		/**
		 * 车辆搜索
		 */
		$("#vehicleSearch").click(function() {//
			var value = $("#queryValue").val();
			conditionSettingNightSet.loadUserVehicle(value);
		})

						/**
						 * 点击新增选项卡
						 */
		$("#saveOrUpdateTab").click(function() {
			 $("#queryValue").val("");
			 $(".queryCarCont").html("");
			conditionSettingNightSet.alarmId = '';
			conditionSettingNightSet.loadUserVehicle();
			//$(".queryCarCont").html(conditionSettingNightSet.allVehicleHtml);
			
			
			
			

		/*	$(".resultCarCont li div").text("");
			$("#name").val("");
			$("#analysegroupId ").val("");
			$("#speedThreshold").val("");
			$("#remark").val("");
			$("#timeThreshold").val("");
			$(".resultCarCont li").remove();

			$("#commandContent").val("");
			$("#remindType2").attr("checked", "checked");

			$("#wechart").attr("checked", "");
			$("#messager_notify").attr("checked", "")
			$("#inputStartDate").val("02:00:00");
			$("#inputEndDate").val("05:00:00");*/
			$("#remindInterval").val("");
			$(".resultCarCont li div").text("");
			$("#name").val("");
			$("#analysegroupId ").val("");
			$("#speedThreshold").val("");
			$("#remark").val("");
			$("#timeThreshold").val("");
			$(".resultCarCont li").remove();
			$("#commandContent").val("");
			$("#remindType2").attr("checked", "checked");
			$("#wechart").attr("checked", false);
			$("#messager_notify").attr("checked", false)
			$("#allVehicle").attr("checked", "checked");
			$("#inputStartDate").val("02:00:00");
			$("#inputEndDate").val("05:00:00");
			var obj = document.getElementById("vehicleTab");
			obj.style.display = "none";
			
		})
        $("#saveNightSetBtn").click(function() {
			
        	conditionSettingNightSet.saveNightSet();
        	
			
		});
		$("#comDelBtn").click(function(){
			conditionSettingNightSet.deleteNightSetComf();
			conditionSettingNightSet.deleteNightSetVehicle();
		})
		$(".queryCarCont ").on("click", "li", function() {

			$(this).toggleClass("active");
		})
		$(".queryCarCont ").on("dblclick", "li", function() {
			var _this = this;
			$(this).remove();
			//$(this).addClass("active");
			$(".resultCarCont").prepend(this);

		})

		$(".resultCarCont").on("click", "li", function() {
			$(this).toggleClass("active");
		})
		$(".resultCarCont ").on("dblclick", "li", function() {
			var _this = this
			$(_this).remove();
			$(".queryCarCont").prepend(_this);

		})
		$(".go").click(function() {
			$(".resultCarCont").append($(".queryCarCont .active"));
			$(".queryCarCont .active").remove();
		})
		$(".back").click(function() {
			$(".queryCarCont").prepend($(".resultCarCont .active"));
			$(".resultCarCont .active").remove();
		})

		$("#validateBtn").click(function() {
			$("velidateModal").modal("hide");
		})
		$('#velidateModal').on('hidden', function() {
			$("#content p").remove();
		})
	},

	quaryTab : function() {
		var index = 0;
		var _this = this;
		var func = function() {
			$('.tab-alarm-cont-list').hide().eq(index).show();
			$(this).addClass('active');
			$(this).siblings().removeClass('active');
			$('.date-alarm').find('.line').css({
				'left' : 100 * index
			});
		};
		$('.date-alarm').find('.list').click(function() {
			index = $(this).index();
			func();
		});
		return {
			'index' : index,
			reflash : function(i) {
				index = i;
				func();
			}
		}
	},

	/**
	 * 车辆选择
	 */
	choseCar : function() {

		$('#selectCar').select2();
	},
	
	updateNightSet : function(id, analyseGroupsID) {
		
		
		conditionSettingNightSet.alarmId = id;
		$(".queryCarCont").html("");
		//setTimeout(conditionSettingNightSet.queryOtherVehicle(analyseGroupsID),1000);
		conditionSettingNightSet.loadUserVehicle();
		$("#chooseVehicle").attr("checked", "checked");
		var obj = document.getElementById("vehicleTab");
		obj.style.display = "block";
		
				/**
				 * 回显已分配的车辆
				 */
		$.ajax({
			type : 'post',
			async : false,
			url : appCtx + 'reportset/vehicleSpeed/queryVehicle.html',
			cache : false,
			data : {
				"id" : analyseGroupsID
			},
			dataType : "JSON",
			success : function(data) {
				var arr = data.rows;
				var size = arr.length;
				var vehicleArr = conditionSettingNightSet.vehicleArr
				var vehicleSize = conditionSettingNightSet.vehicleSize
				$(".resultCarCont li").remove();
				var html = "";
				for (var i = 0; i < size; i++) {
						html += '<li class="' + 'list "><a class=' + '"db'
								+ '" href=' + '"javascript:;' + '">'
								+ arr[i].registrationNo + '</a> <div style="'
								+ 'display: none"' + '>' + arr[i].vehicleId
								+ ',</div></li>';
				}
				$(".resultCarCont").html(html);

			}

		});
		$.ajax({
			type : 'post',
			async : false,
			url : appCtx + "reportset/nightSet/updateNightSet.html",
			cache : false,
			data : {
				"id" : id
			},
			dataType : "JSON",
			success : function(data) {
				var data = data.data;
				$("#name").val(data.name);
				$("#remark").val(data.remark);
				$("#speedThreshold ").val(data.speedThreshold);
				
				var timeThreshold=(data.timeThreshold)/60;
				   var tnum=timeThreshold.toString().indexOf(".")
					if(tnum>0){
						timeThreshold=timeThreshold.toFixed(2);
				}
				
				$("#timeThreshold ").val(timeThreshold);
				$("#analysegroupId ").val(data.analyseGroupsID);
				$("#inputStartDate").val(data.startTime);
				$("#inputEndDate").val(data.endTime);
				

				var remindInterval=(data.remindInterval)/60;
				
				   var rnum=remindInterval.toString().indexOf(".")
					if(rnum>0){
						remindInterval=remindInterval.toFixed(2);
				}
				
				
				$("#remindInterval").val(remindInterval);
				var periodValue = data.periodValue;

				$("#commandContent").val(data.commandContent);

				if (data.remindType == 2) {
					$("#remindType2").attr("checked", "checked");
				} else if (data.remindType == 3) {
					$("#remindType3").attr("checked", "checked");
				} else {
					$("#remindType4").attr("checked", "checked");
				}
				if (data.is_weixin_notify == 1) {
					$("#wechart").attr("checked", "checked")
				} else {
					$("#wechart").attr("checked", false);
				}
				;
				if (data.is_sms_notify == 1) {
					$("#messager_notify").attr("checked", "checked")
				} else {
					$("#messager_notify").attr("checked", false);
				}
			}

		});
		conditionSettingNightSet.queryTabInit.reflash(1);

	},
	saveNightSet : function() {
		
    var param=conditionSettingNightSet.getParams();
    
    if(!conditionSettingNightSet.validata){
    	global.ajaxLoading.pop(conditionSettingNightSet.msg, false, 2000);
    	return;
    }
    
    
    
		var msg="";
		$.ajax({
			type : 'post',
			async : false,
			url : appCtx + "reportset/nightSet/saveNightSet.html",
			cache : false,
			data : param,
			dataType : "JSON",
			success : function(data) {
				msg=data.msg;
				$("#annal").flexReload();

				
				

			}

		});
		
		global.ajaxLoading.pop(msg, false, 1000);
		
		conditionSettingNightSet.queryTabInit.reflash(0);

		
	},deleteNightSetComf:function(){
		$.ajax({
			type : 'post',
			async : false,
			url : appCtx + "reportset/nightSet/deleteNightSet.html",
			cache : false,
			data : {
				"id" : conditionSettingNightSet.delId,
				"analyseGroupsId" : conditionSettingNightSet.delAnalyseGroupsId
			},
			dataType : "JSON",
			success : function(data) {
				
			}

		});
		$("#myModalDel").modal("hide");
		$("#annal").flexReload();
	},
	/**
	 * 删除超速设置
	 */

	deleteNightSet : function(id, analyseGroupsId) {
		conditionSettingNightSet.delId=id;
		conditionSettingNightSet.delAnalyseGroupsId=analyseGroupsId;
	},deleteNightSetVehicle:function(analyseGroupsId){
		$.ajax({
			type : 'post',
			async : true,
			url : appCtx + "reportset/nightSet/deleteNightSetVehicle.html",
			cache : false,
			data : {
				"analyseGroupsId" : conditionSettingNightSet.delAnalyseGroupsId
			},
			dataType : "JSON",
			success : function(data) {
				
			}

		});
		
	},
	/**
	 * 查看车辆
	 * 
	 * @param id
	 */
	queryVehicle : function(id) {

		$('#myModal').on('hidden', function() {
			$("#ownCarCont li").remove();
		})

		$
				.ajax({
					type : 'post',
					async : false,
					url : appCtx + "reportset/vehicleSpeed/queryVehicle.html",
					cache : false,
					data : {
						"id" : id
					},
					dataType : "JSON",
					success : function(data) {
						var vehicleArr = data.rows;
						var size = vehicleArr.length;
						var html = '<li class="list "><a class="db" href="javascript:;"><span style="color: blue;">车牌号:</span></a></li> ';
						for (var i = 0; i < size; i++) {
							html += '<li class="' + 'list "><a class=' + '"db'
									+ '" href=' + '"javascript:;' + '">'
									+ vehicleArr[i].registrationNo//
									+ '</a> <div style="' + 'display: none"'
									+ '>'+ vehicleArr[i].vehicleId + ',</div></li>'
						}
						$(".ownCarCont").html(html);
					}

				});

	},
	annal : function() {
		/**
		 * 操作列
		 */
		function formmatOperation(iD, analyseGroupsID) {
			var handleStr = "";

			handleStr = '<a href="javascript:void(0)" onclick="conditionSettingNightSet.updateNightSet('//
					+ iD+ ','+ analyseGroupsID
					+ ')" title=""'+ '" >  <span style='
					+ '"color: blue;'+ '">修改</span></a>'
					+ '&nbsp;&nbsp;<a href="#myModalDel" onclick="conditionSettingNightSet.deleteNightSet('//
					+ iD+ ','+ analyseGroupsID+ ')" '+ 'data-toggle="'
					+ 'modal"'+ 'title="'+ '" >  <span style=' + '"color: blue;' + '">删除</span></a>';//

			return handleStr;
		}
		;
		function formmatVehicle(analyseGroupsID) {
			var handleStr = "";
			handleStr = '<a href="' + '#myModal' + '"  role=' + '"button'//
					+ '" onclick=' + '"conditionSettingNightSet.queryVehicle('//
					+ analyseGroupsID + ')" data-toggle=' + '"modal'//
					+ '"><span style=' + '"color: blue;' + '"</span>查看车辆</a>';//
			return handleStr;
		}
		;

		$("#annal").flexigrid({
			dataType : 'json',
			url : appCtx + 'reportset/nightSet/queryAllNightSet.html',
			params : [],
			contentType : "application/json; charset=utf-8",
			method : 'POST',
			widthType:'%',
	        width:$('.tab-alarm-cont').width(),
			colModel : [ {
				display : '条件名称',
				name : 'name',
				width : 8,
				sortable : false,
				// hide:true,
				align : 'left',
				checked : 'checked',
				paramcolnames : [ 'name'],
				handlefunction : function(name) {
							return '<a class=\"textOverFlow\" href=\"javascript:;\" title=\"'+name+'" >'+name+'</a>';
						}
			}, {
				display : '速度门限',
				name : 'speedThreshold',
				width : 5,
				sortable : false,
				align : 'left',
			}, {
				display : '时间门限',
				name : 'timeThreshold',
				width : 5,
				sortable : false,
				align : 'left',
				paramcolnames : [ 'timeThreshold' ],
				handlefunction : function(timeThreshold) {
					if(timeThreshold){
						   timeThreshold=timeThreshold/60;
						   var num=timeThreshold.toString().indexOf(".")
						if(num>0){
							timeThreshold=timeThreshold.toFixed(2);
					}
				}
					return timeThreshold;
				}
			}, {
				display : '时间范围',
				name : 'startTime',
				width : 11,
				sortable : false,
				align : 'left',
				paramcolnames : [ 'startTime', 'endTime' ],
				handlefunction : function(startTime, endTime) {
					return startTime + "至" + endTime;
				}
			},{
				display : '时间间隔',
				name : 'remindInterval',
				width : 5,
				sortable : false,
				align : 'left',
				paramcolnames : [ 'remindInterval' ],
				handlefunction : function(remindInterval) {
					if(remindInterval){
						remindInterval=remindInterval/60;
						   var num=remindInterval.toString().indexOf(".")
						if(num>0){
							remindInterval=remindInterval.toFixed(2);
					}
				}
					return remindInterval;
				}
			} ,{
				display : '提醒方式',
				name : 'remindType',
				width : 10,
				sortable : false,
				align : 'left',
				paramcolnames : [ 'remindType' ],
				handlefunction : function(remindType) {
					if (remindType == 2) {
						remindType = "平台报警";
					} else if (remindType == 3) {
						remindType = "TTS语音播报"
					} else {
						remindType = "平台/TTS语音播报"
					}
					return remindType;
				}
			}, {
				display : '微信通知',
				name : 'is_weixin_notify',
				width : 6,
				sortable : false,
				align : 'center',
				paramcolnames : [ 'is_weixin_notify' ],
				handlefunction : function(is_weixin_notify) {
					if (is_weixin_notify == 0) {
						is_weixin_notify = "否";
					} else {
						is_weixin_notify = "是";
					}
					return is_weixin_notify;
				}
			}, {
				display : '短信通知',
				name : 'is_sms_notify',
				width : 6,
				sortable : false,
				align : 'center',
				paramcolnames : [ 'is_sms_notify' ],
				handlefunction : function(is_sms_notify) {
					if (is_sms_notify == 0) {
						is_sms_notify = "否";
					} else {
						is_sms_notify = "是";
					}
					return is_sms_notify;
				}
			}, {
				display : '监控车辆',
				name : 'analyseGroupsID',
				width : 6,
				sortable : false,
				align : 'center',

				paramcolnames : [ 'analyseGroupsID' ],
				handlefunction : formmatVehicle,
			}, {
				display : '创建时间',
				name : 'createDatetime',
				width :11,
				sortable : false,
				align : 'center',
				paramcolnames : [ 'createDatetime' ],
				handlefunction : function(createDatetime) {
					var size=createDatetime.split(".").length;
					if(size>1){
					createDatetime=createDatetime.split(".")[0];
					
					}
					return createDatetime;
				}
				
			},{
				display : '播报内容',
				name : 'commandContent',
				width : 10,
				sortable : false,
				align : 'left',
				paramcolnames : [ 'commandContent' ],
			    handlefunction : function(commandContent) {
						return '<a class=\"textOverFlow\" href=\"javascript:;\" title=\"'+commandContent+'" >'+commandContent+'</a>';
					}

			}, {
				display : '备注',
				name : 'remark',
				width : 9,
				sortable : false,
				align : 'left',
				paramcolnames : [ 'remark' ],
			    handlefunction : function(remark) {
						return '<a class=\"textOverFlow\" href=\"javascript:;\" title=\"'+remark+'" >'+remark+'</a>';
					}
			}, {
				display : '操作',
				name : 'operation',
				width : 8,
				sortable : false,
				align : 'left',
				paramcolnames : [ 'iD', 'analyseGroupsID' ],
				handlefunction : formmatOperation
			} ],

			resizable : false,
			sortname : "terminalId",// 第一次加载数据时排序列
			sortorder : "desc",// 第一次加载数据时排序类型
			usepager : false,// 是否分页，默认为true。
			showToggleBtn : false,
			showTableToggleBtn : false,// 是否显示收起/打开按钮,默认不显示。
			// useRp : true,// 是否可以动态设置每页显示的结果数，默认为false。
			// rp : 10,// 每页记录数，默认为10
			// rpOptions: [10,50,100,300,500], // 每页结果数
			checkbox : true,// 是否要多选框,默认为false。
			rowId : 'terminalId',// 多选框绑定行的id,只有checkbox :
			// true时才有效。
			singleSelect : true,
			height : nav.whiceH[1] - 130, // 表格高度
			titDisplay : false,
			onSuccess : function(data) {
			},
			onSelectRow : function(row) {

			},
			onSelectAllRow : function(data) {

			}

		});
	},getParams:function(){
		conditionSettingNightSet.msg="";
		conditionSettingNightSet.validata=true;
		var s = Date.parse(new Date());
		var ids = $(".resultCarCont li div").text();
		if (!ids) {
			ids = conditionSettingNightSet.allVehicleId;
		}
		if ($("#allVehicle").attr("checked") == "checked") {
			ids = conditionSettingNightSet.allVehicleId;
		}
		var name = $.trim($("#name").val());
		
		var timeThreshold=$.trim($("#timeThreshold").val());
		if(timeThreshold){
			timeThreshold=(timeThreshold*60).toFixed(0);
		}
		var speedThreshold = $.trim($("#speedThreshold ").val());
		if (!name) {
			
			conditionSettingNightSet.msg="【请填写条件名称】";
			conditionSettingNightSet.validata=false;
			
			/*$("#content").append("  <p>请填写条件名称</p> ")
			$("#velidateModal").modal("show");
			return;*/
		}
		if (!timeThreshold) {
			timeThreshold = 600
		}
		if (!speedThreshold) {
			conditionSettingNightSet.msg="【请填速度门限】";
			conditionSettingNightSet.validata=false;
			/*$("#content").append("  <p>请填速度门限</p> ")
			$("#velidateModal").modal("show");
			return;*/
		}
		if(speedThreshold&&speedThreshold>120){
			conditionSettingNightSet.msg="【请填入0-120门限速度】";
			conditionSettingNightSet.validata=false;
			/*$("#content").append("  <p>请填入0-120门限速度</p> ")
			$("#velidateModal").modal("show");
			return;*/
		}
		var remindInterval=$("#remindInterval").val();
		if(remindInterval){
			remindInterval=(remindInterval*60).toFixed(0);
		}
		if(!remindInterval){
			remindInterval=600;
		}
		var start = $("#inputStartDate").val();
		var end = $("#inputEndDate").val();
		var startTime = start;
		var endTime = end;
		if (!startTime) {
			startTime = "02:00:00";
		}
		if (!endTime) {
			endTime = "05:00:00";
		}
		if (end < start) {
			startTime = end;
			endTime = start;
		}
		var remark = $.trim($("#remark").val());
		var analysegroupId = $("#analysegroupId").val();
		var commandContent = $.trim($("#commandContent").val());
		if (!commandContent) {
			commandContent = "此时间段禁止行车，请找地方停车休息!";
		}
		var remindType = 2;
		if ($("#remindType3").attr("checked") == "checked") {
			remindType = 3;
		}
		if ($("#remindType4").attr("checked") == "checked") {
			remindType = 4;
		}
		var wechart = 0;
		if ($("#wechart").attr("checked") == "checked") {
			wechart = 1
		}
		var messager_notify = 0;
		if ($("#messager_notify").attr("checked") == "checked") {
			messager_notify = 1
		}
		param = {
			"Name" : name,
			"TimeThreshold" : timeThreshold,
			"SpeedThreshold" : speedThreshold,
			"PeriodValue" : "每日",
			"Remark" : remark,
			"ids" : ids,
			//"action" : action,
			"AnalyseGroupsID" : analysegroupId,
			"RemindType" : remindType,
			"Is_weixin_notify" : wechart,
			"Is_sms_notify" : messager_notify,
			"CommandContent" : commandContent,
			"ID" : conditionSettingNightSet.alarmId,
			"StartTime" : startTime,
			"RemindInterval" : remindInterval,
			"EndTime" : endTime
		}
		var e= Date.parse(new Date());
		
		console.log("获取参数用掉的时间:"+(e-s));
		return param;
		
	
		
	}
};

conditionSettingNightSet.init();